package view;



import model.Game;
import model.Word;
import control.GameGrillListener;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

import javax.swing.JPanel;
import javax.swing.event.*;

import devintAPI.*;


public class GameGrill extends FenetreAbstraite implements ActionListener, KeyListener{
	
	private static final int NBLINE=5;
	private static final int NBCUMUN=8;
	
	private  int nbButton;
	private int nbclic=0;
	private int nbEnter=0;
	private JButton bSyllables[];
	private int currentButton;
	
	private JLabel labelScreen=new JLabel();
	
	private JPanel panelSyllable=new JPanel();
	private JPanel panelScreen=new JPanel();
	private JPanel panel;
	
	private Dimension dim=new Dimension(300,300);
	
	private Container contentPane;
	
	private JButton b1,b2;
	
	private Game game;
	private ArrayList<String> syllables;
	
	
	
	// attributs des textes et des boutons
		// � red�finir dans la classe concr�te si vous le souhaitez
		// en gardant de forts contrastes et peu de couleurs diff�rentes
		protected Font fonteBouton;
		protected Color foregroundColor;
		protected Color backgroundColor;

	
	public GameGrill(Game game) {
		 super("");
         this.setResizable(false);
         
      // on r�cup�re les pr�f�rences
 		Preferences pref = Preferences.getData();
 		foregroundColor = pref.getCurrentForegroundColor();
 		backgroundColor = pref.getCurrentBackgroundColor();
 		
 		//foregroundColor=new Color(10, 0, 150);
 		//backgroundColor=Color.WHITE;
 		
         contentPane=getContentPane();
         panel=new JPanel();
         
         panelSyllable.setPreferredSize(dim);
         panelSyllable.setLayout(new GridLayout(NBLINE, NBCUMUN, 6, 4));
         panelSyllable.setBackground(Color.blue);
         
         panel.add(panelScreen,BorderLayout.NORTH);
         panel.add(panelSyllable, BorderLayout.CENTER);
        
         contentPane.add(panel);
         
         
         this.game=game;
         
         initComposant();      
         
         this.setVisible(true);
         this.requestFocus();
         currentButton=-1;
	}
	
	public void initComposant() {
		
		syllables=game.getGameSyllables();
		nbButton=syllables.size();
		bSyllables=new JButton[nbButton];
	//	bSyllables=new JButton[40];
		
		panelScreen.setBackground(Color.CYAN);
		panelScreen.setPreferredSize(new Dimension(1500,200) );
		panelSyllable.setPreferredSize(new Dimension(1500,600));
		
		panelScreen.setBorder(BorderFactory.createEtchedBorder());
		labelScreen.setHorizontalTextPosition(JLabel.LEFT);
		labelScreen.setText("");
		panelScreen.add(labelScreen);
		
		
		for(int i=0; i<nbButton; i++) {
			bSyllables[i]=new JButton(syllables.get(i));
			bSyllables[i].setPreferredSize(new Dimension(70,60));
			bSyllables[i].setBackground(Color.ORANGE);
			panelSyllable.add(bSyllables[i]);
			bSyllables[i].addActionListener(this);
		}
	}

	@Override
	public void actionPerformed(ActionEvent ev) {
		
		if(game.getNbWordFound()!=game.getListWords().size()) {
			//panelSyllable.setEnabled(false);
			for(int i=0; i<bSyllables.length;i++)
				bSyllables[i].setEnabled(false);
			contentPane.validate();
		}
		else {
			nbclic+=1;
			if(nbclic==1) {
				b1=(JButton)ev.getSource();
				System.out.println("bouton1:"+nbclic);
			}
			if(nbclic==2) {
				System.out.println("bouton2:"+nbclic);
				nbclic=0;
				b2=(JButton)ev.getSource();
				String b1Name=b1.getActionCommand();
				String b2Name=b2.getActionCommand();
				System.out.println(b1Name+b2Name);
				boolean aWord=game.containsWordName(b1Name+b2Name);
				if(aWord) {
					String s=b1Name + b2Name;
					//String s=b1.getActionCommand()+b2.getActionCommand();
					labelScreen.setText(labelScreen.getText()+"  "+s);
					b1.setText(" ");
					b2.setText(" ");
				
					b1.setEnabled(false);
					b2.setEnabled(false);
					b1.revalidate();
					b2.revalidate();	
					contentPane.validate();
				}
				
			}
			this.requestFocus();
		}
		
	}
	

	public void keyPressed(KeyEvent e) {
		// gestion du son d'accueil, des touches F1, ESC, F3 et F4
		// g�r� par DevintFrameListener
		super.keyPressed(e);
		// enter = s�lectionner l'option
		if (e.getKeyCode() == KeyEvent.VK_ENTER) {
			nbEnter+=1;
			if(nbEnter==1) {
				b1=bSyllables[currentButton];
			}
			if(nbEnter==2) {
				nbEnter=0;
				b2=bSyllables[currentButton];
				
				String b1Name=b1.getActionCommand();
				String b2Name=b2.getActionCommand();
				System.out.println(b1Name+b2Name);
				boolean aWord=game.containsWordName(b1Name+b2Name);
				if(aWord) {
					
					String s=b1Name + b2Name;
					Word w=new Word(s);
					w.addSyllable(b1Name);
					w.addSyllable(b2Name);
					game.addToWordFound(w);
					game.incNbWordFound();
					
					//String s=b1.getActionCommand()+b2.getActionCommand();
					labelScreen.setText(labelScreen.getText()+"  "+s);
					b1.setText(" ");
					b2.setText(" ");
					
					b1.setEnabled(false);
					b2.setEnabled(false);
					b1.revalidate();
					b2.revalidate();	
					contentPane.validate();
				}
				}
			
						// TODO Auto-generated method stub

			// m�thode � rendre concr�te par h�ritage
			//lancerOption(currentButton);  
		}
		// se d�placer dans les options vers le bas
		if (e.getKeyCode() == KeyEvent.VK_DOWN) {
			if (currentButton == -1) {
				currentButton = 0;
				setFocusedButton(currentButton);
			} else {
				unFocusedButton(currentButton);
				currentButton = (currentButton + 8) % nbButton;
				setFocusedButton(currentButton);
			}
		}
		// se d�placer dans les options vers le haut
		if (e.getKeyCode() == KeyEvent.VK_UP) {
			if (currentButton < 0) {
				currentButton =  nbButton + currentButton;
				setFocusedButton(currentButton);
			} else {
				unFocusedButton(currentButton);
				currentButton = currentButton - 8 ;
				if (currentButton <= -1)
					currentButton = nbButton + currentButton;
				setFocusedButton(currentButton);
			}
		}
		if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
			if (currentButton == -1) {
				currentButton = 0;
				setFocusedButton(currentButton);
			} else {
				unFocusedButton(currentButton);
				currentButton = (currentButton + 1) % nbButton;
				setFocusedButton(currentButton);
			}
		}
		
		if (e.getKeyCode() == KeyEvent.VK_LEFT) {
			if (currentButton == -1) {
				currentButton = 0;
				setFocusedButton(currentButton);
			} else {
				unFocusedButton(currentButton);
				currentButton = (currentButton - 1) % nbButton;
				if(currentButton==-1)
					currentButton=nbButton-1;
				setFocusedButton(currentButton);
			}
		}
		}
		
		
		
		
		// mettre le focus sur un boutton
		private void setFocusedButton(int i) {
			voix.playShortText(bSyllables[i].getText());
			bSyllables[i].setBackground(backgroundColor);
			bSyllables[i].setForeground(foregroundColor);
		}

		// enlever le focus d'une option
	private void unFocusedButton(int i) {
			bSyllables[i].setBackground(Color.ORANGE);
	}
	@Override
	protected void init() {
		
		// TODO Auto-generated method stub
		
	}

	@Override
	protected  String wavAide() {
		return "../ressources/sons/aide.wav";
	}
	@Override
	public void changeColor() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected String wavAccueil() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String wavRegleJeu() {
		// TODO Auto-generated method stub
		return null;
	}
	
}
